home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 10 / AACD 10.iso / AACD / Games / MAME / src / sound / pokey.c < prev    next >
C/C++ Source or Header  |  2000-04-23  |  47KB  |  1,487 lines

  1. /*****************************************************************************
  2.  *
  3.  *    POKEY chip emulator 4.3
  4.  *    Copyright (c) 2000 by The MAME Team
  5.  *
  6.  *    Based on original info found in Ron Fries' Pokey emulator,
  7.  *    with additions by Brad Oliver, Eric Smith and Juergen Buchmueller,
  8.  *    paddle (a/d conversion) details from the Atari 400/800 Hardware Manual.
  9.  *    Polynome algorithms according to info supplied by Perry McFarlane.
  10.  *
  11.  *    This code is subject to the MAME license, which besides other
  12.  *  things means it is distributed as is, no warranties whatsoever.
  13.  *    For more details read the readme.txt that comes with MAME.
  14.  *
  15.  *    4.3:
  16.  *    - for POT inputs returning zero, immediately assert the ALLPOT
  17.  *      bit after POTGO is written, otherwise start trigger timer
  18.  *      depending on SK_PADDLE mode, either 1-228 scanlines or 1-2
  19.  *      scanlines, depending on the SK_PADDLE bit of SKCTL.
  20.  *    4.2:
  21.  *    - half volume for channels which are inaudible (this should be
  22.  *      close to the real thing).
  23.  *    4.1:
  24.  *    - default gain increased to closely match the old code.
  25.  *    - random numbers repeat rate depends on POLY9 flag too!
  26.  *    - verified sound output with many, many Atari 800 games,
  27.  *      including the SUPPRESS_INAUDIBLE optimizations.
  28.  *    4.0:
  29.  *    - rewritten from scratch.
  30.  *    - 16bit stream interface.
  31.  *    - serout ready/complete delayed interrupts.
  32.  *    - reworked pot analog/digital conversion timing.
  33.  *    - optional non-indexing pokey update functions.
  34.  *
  35.  *****************************************************************************/
  36.  
  37. #include "driver.h"
  38.  
  39. /*
  40.  * Defining this produces much more (about twice as much)
  41.  * but also more efficient code. Ideally this should be set
  42.  * for processors with big code cache and for healthy compilers :)
  43.  */
  44. #ifndef BIG_SWITCH
  45. #ifndef HEAVY_MACRO_USAGE
  46. #define HEAVY_MACRO_USAGE   1
  47. #endif
  48. #else
  49. #define HEAVY_MACRO_USAGE    BIG_SWITCH
  50. #endif
  51.  
  52. #define SUPPRESS_INAUDIBLE    1
  53.  
  54. /* Four channels with a range of 0..32767 and volume 0..15 */
  55. //#define POKEY_DEFAULT_GAIN (32767/15/4)
  56.  
  57. /*
  58.  * But we raise the gain and risk clipping, the old Pokey did
  59.  * this too. It defined POKEY_DEFAULT_GAIN 6 and this was
  60.  * 6 * 15 * 4 = 360, 360/256 = 1.40625
  61.  * I use 15/11 = 1.3636, so this is a little lower.
  62.  */
  63. #define POKEY_DEFAULT_GAIN (32767/11/4)
  64.  
  65. #define VERBOSE         0
  66. #define VERBOSE_SOUND    0
  67. #define VERBOSE_TIMER    0
  68. #define VERBOSE_POLY    0
  69. #define VERBOSE_RAND    0
  70.  
  71. #if VERBOSE
  72. #define LOG(x) logerror x
  73. #else
  74. #define LOG(x)
  75. #endif
  76.  
  77. #if VERBOSE_SOUND
  78. #define LOG_SOUND(x) logerror x
  79. #else
  80. #define LOG_SOUND(x)
  81. #endif
  82.  
  83. #if VERBOSE_TIMER
  84. #define LOG_TIMER(x) logerror x
  85. #else
  86. #define LOG_TIMER(x)
  87. #endif
  88.  
  89. #if VERBOSE_POLY
  90. #define LOG_POLY(x) logerror x
  91. #else
  92. #define LOG_POLY(x)
  93. #endif
  94.  
  95. #if VERBOSE_RAND
  96. #define LOG_RAND(x) logerror x
  97. #else
  98. #define LOG_RAND(x)
  99. #endif
  100.  
  101. #define CHAN1    0
  102. #define CHAN2    1
  103. #define CHAN3    2
  104. #define CHAN4    3
  105.  
  106. #define TIMER1    0
  107. #define TIMER2    1
  108. #define TIMER4    2
  109.  
  110. /* values to add to the divisors for the different modes */
  111. #define DIVADD_LOCLK        1
  112. #define DIVADD_HICLK        4
  113. #define DIVADD_HICLK_JOINED 7
  114.  
  115. /* AUDCx */
  116. #define NOTPOLY5    0x80    /* selects POLY5 or direct CLOCK */
  117. #define POLY4        0x40    /* selects POLY4 or POLY17 */
  118. #define PURE        0x20    /* selects POLY4/17 or PURE tone */
  119. #define VOLUME_ONLY 0x10    /* selects VOLUME OUTPUT ONLY */
  120. #define VOLUME_MASK 0x0f    /* volume mask */
  121.  
  122. /* AUDCTL */
  123. #define POLY9        0x80    /* selects POLY9 or POLY17 */
  124. #define CH1_HICLK    0x40    /* selects 1.78979 MHz for Ch 1 */
  125. #define CH3_HICLK    0x20    /* selects 1.78979 MHz for Ch 3 */
  126. #define CH12_JOINED 0x10    /* clocks channel 1 w/channel 2 */
  127. #define CH34_JOINED 0x08    /* clocks channel 3 w/channel 4 */
  128. #define CH1_FILTER    0x04    /* selects channel 1 high pass filter */
  129. #define CH2_FILTER    0x02    /* selects channel 2 high pass filter */
  130. #define CLK_15KHZ    0x01    /* selects 15.6999 kHz or 63.9211 kHz */
  131.  
  132. /* IRQEN (D20E) */
  133. #define IRQ_BREAK    0x80    /* BREAK key pressed interrupt */
  134. #define IRQ_KEYBD    0x40    /* keyboard data ready interrupt */
  135. #define IRQ_SERIN    0x20    /* serial input data ready interrupt */
  136. #define IRQ_SEROR    0x10    /* serial output register ready interrupt */
  137. #define IRQ_SEROC    0x08    /* serial output complete interrupt */
  138. #define IRQ_TIMR4    0x04    /* timer channel #4 interrupt */
  139. #define IRQ_TIMR2    0x02    /* timer channel #2 interrupt */
  140. #define IRQ_TIMR1    0x01    /* timer channel #1 interrupt */
  141.  
  142. /* SKSTAT (R/D20F) */
  143. #define SK_FRAME    0x80    /* serial framing error */
  144. #define SK_OVERRUN    0x40    /* serial overrun error */
  145. #define SK_KBERR    0x20    /* keyboard overrun error */
  146. #define SK_SERIN    0x10    /* serial input high */
  147. #define SK_SHIFT    0x08    /* shift key pressed */
  148. #define SK_KEYBD    0x04    /* keyboard key pressed */
  149. #define SK_SEROUT    0x02    /* serial output active */
  150.  
  151. /* SKCTL (W/D20F) */
  152. #define SK_BREAK    0x80    /* serial out break signal */
  153. #define SK_BPS        0x70    /* bits per second */
  154. #define SK_FM        0x08    /* FM mode */
  155. #define SK_PADDLE    0x04    /* fast paddle a/d conversion */
  156. #define SK_RESET    0x03    /* reset serial/keyboard interface */
  157.  
  158. #define DIV_64        28         /* divisor for 1.78979 MHz clock to 63.9211 kHz */
  159. #define DIV_15        114      /* divisor for 1.78979 MHz clock to 15.6999 kHz */
  160.  
  161. struct POKEYregisters {
  162.     INT32 counter[4];        /* channel counter */
  163.     INT32 divisor[4];        /* channel divisor (modulo value) */
  164.     UINT32 volume[4];        /* channel volume - derived */
  165.     UINT8 output[4];        /* channel output signal (1 active, 0 inactive) */
  166.     UINT8 audible[4];        /* channel plays an audible tone/effect */
  167.     UINT32 samplerate_24_8; /* sample rate in 24.8 format */
  168.     UINT32 samplepos_fract; /* sample position fractional part */
  169.     UINT32 samplepos_whole; /* sample position whole part */
  170.     UINT32 polyadjust;        /* polynome adjustment */
  171.     UINT32 p4;              /* poly4 index */
  172.     UINT32 p5;              /* poly5 index */
  173.     UINT32 p9;              /* poly9 index */
  174.     UINT32 p17;             /* poly17 index */
  175.     UINT32 r9;                /* rand9 index */
  176.     UINT32 r17;             /* rand17 index */
  177.     UINT32 clockmult;        /* clock multiplier */
  178.     int channel;            /* streams channel */
  179.     void *timer[3];         /* timers for channel 1,2 and 4 events */
  180.     void *rtimer;           /* timer for calculating the random offset */
  181.     void *ptimer[8];        /* pot timers */
  182.     mem_read_handler pot_r[8];
  183.     mem_read_handler allpot_r;
  184.     mem_read_handler serin_r;
  185.     mem_write_handler serout_w;
  186.     void (*interrupt_cb)(int mask);
  187.     UINT8 AUDF[4];          /* AUDFx (D200, D202, D204, D206) */
  188.     UINT8 AUDC[4];            /* AUDCx (D201, D203, D205, D207) */
  189.     UINT8 POTx[8];            /* POTx   (R/D200-D207) */
  190.     UINT8 AUDCTL;            /* AUDCTL (W/D208) */
  191.     UINT8 ALLPOT;            /* ALLPOT (R/D208) */
  192.     UINT8 KBCODE;            /* KBCODE (R/D209) */
  193.     UINT8 RANDOM;            /* RANDOM (R/D20A) */
  194.     UINT8 SERIN;            /* SERIN  (R/D20D) */
  195.     UINT8 SEROUT;            /* SEROUT (W/D20D) */
  196.     UINT8 IRQST;            /* IRQST  (R/D20E) */
  197.     UINT8 IRQEN;            /* IRQEN  (W/D20E) */
  198.     UINT8 SKSTAT;            /* SKSTAT (R/D20F) */
  199.     UINT8 SKCTL;            /* SKCTL  (W/D20F) */
  200. };
  201.  
  202. static struct POKEYinterface intf;
  203. static struct POKEYregisters pokey[MAXPOKEYS];
  204.  
  205. static UINT8 poly4[0x0f];
  206. static UINT8 poly5[0x1f];
  207. static UINT8 *poly9;
  208. static UINT8 *poly17;
  209.  
  210. #define P4(chip)  poly4[pokey[chip].p4]
  211. #define P5(chip)  poly5[pokey[chip].p5]
  212. #define P9(chip)  poly9[pokey[chip].p9]
  213. #define P17(chip) poly17[pokey[chip].p17]
  214.  
  215. /* 128K random values derived from the 17bit polynome */
  216. static UINT8 *rand9;
  217. static UINT8 *rand17;
  218.  
  219. #define SAMPLE    -1
  220.  
  221. #define ADJUST_EVENT(chip)                                                \
  222.     pokey[chip].counter[CHAN1] -= event;                                \
  223.     pokey[chip].counter[CHAN2] -= event;                                \
  224.     pokey[chip].counter[CHAN3] -= event;                                \
  225.     pokey[chip].counter[CHAN4] -= event;                                \
  226.     pokey[chip].samplepos_whole -= event;                                \
  227.     pokey[chip].polyadjust += event
  228.  
  229. #if SUPPRESS_INAUDIBLE
  230.  
  231. #define PROCESS_CHANNEL(chip,ch)                                        \
  232.     int toggle = 0;                                                     \
  233.     ADJUST_EVENT(chip);                                                 \
  234.     /* reset the channel counter */                                     \
  235.     if( pokey[chip].audible[ch] )                                        \
  236.         pokey[chip].counter[ch] = pokey[chip].divisor[ch];                \
  237.     else                                                                \
  238.         pokey[chip].counter[ch] = 0x7fffffff;                            \
  239.     pokey[chip].p4 = (pokey[chip].p4+pokey[chip].polyadjust)%0x0000f;    \
  240.     pokey[chip].p5 = (pokey[chip].p5+pokey[chip].polyadjust)%0x0001f;    \
  241.     pokey[chip].p9 = (pokey[chip].p9+pokey[chip].polyadjust)%0x001ff;    \
  242.     pokey[chip].p17 = (pokey[chip].p17+pokey[chip].polyadjust)%0x1ffff; \
  243.     pokey[chip].polyadjust = 0;                                         \
  244.     if( (pokey[chip].AUDC[ch] & NOTPOLY5) || P5(chip) )                 \
  245.     {                                                                    \
  246.         if( pokey[chip].AUDC[ch] & PURE )                                \
  247.             toggle = 1;                                                 \
  248.         else                                                            \
  249.         if( pokey[chip].AUDC[ch] & POLY4 )                                \
  250.             toggle = pokey[chip].output[ch] == !P4(chip);                \
  251.         else                                                            \
  252.         if( pokey[chip].AUDCTL & POLY9 )                                \
  253.             toggle = pokey[chip].output[ch] == !P9(chip);                \
  254.         else                                                            \
  255.             toggle = pokey[chip].output[ch] == !P17(chip);                \
  256.     }                                                                    \
  257.     if( toggle )                                                        \
  258.     {                                                                    \
  259.         if( pokey[chip].audible[ch] )                                    \
  260.         {                                                                \
  261.             if( pokey[chip].output[ch] )                                \
  262.                 sum -= pokey[chip].volume[ch];                            \
  263.             else                                                        \
  264.                 sum += pokey[chip].volume[ch];                            \
  265.         }                                                                \
  266.         pokey[chip].output[ch] ^= 1;                                    \
  267.     }                                                                    \
  268.     /* is this a filtering channel (3/4) and is the filter active? */    \
  269.     if( pokey[chip].AUDCTL & ((CH1_FILTER|CH2_FILTER) & (0x10 >> ch)) ) \
  270.     {                                                                   \
  271.         if( pokey[chip].output[ch-2] )                                    \
  272.         {                                                               \
  273.             pokey[chip].output[ch-2] = 0;                                \
  274.             if( pokey[chip].audible[ch] )                                \
  275.                 sum -= pokey[chip].volume[ch-2];                        \
  276.         }                                                               \
  277.     }                                                                   \
  278.  
  279. #else
  280.  
  281. #define PROCESS_CHANNEL(chip,ch)                                        \
  282.     int toggle = 0;                                                     \
  283.     ADJUST_EVENT(chip);                                                 \
  284.     /* reset the channel counter */                                     \
  285.     pokey[chip].counter[ch] = p[chip].divisor[ch];                        \
  286.     pokey[chip].p4 = (pokey[chip].p4+pokey[chip].polyadjust)%0x0000f;    \
  287.     pokey[chip].p5 = (pokey[chip].p5+pokey[chip].polyadjust)%0x0001f;    \
  288.     pokey[chip].p9 = (pokey[chip].p9+pokey[chip].polyadjust)%0x001ff;    \
  289.     pokey[chip].p17 = (pokey[chip].p17+pokey[chip].polyadjust)%0x1ffff; \
  290.     pokey[chip].polyadjust = 0;                                         \
  291.     if( (pokey[chip].AUDC[ch] & NOTPOLY5) || P5(chip) )                 \
  292.     {                                                                    \
  293.         if( pokey[chip].AUDC[ch] & PURE )                                \
  294.             toggle = 1;                                                 \
  295.         else                                                            \
  296.         if( pokey[chip].AUDC[ch] & POLY4 )                                \
  297.             toggle = pokey[chip].output[ch] == !P4(chip);                \
  298.         else                                                            \
  299.         if( pokey[chip].AUDCTL & POLY9 )                                \
  300.             toggle = pokey[chip].output[ch] == !P9(chip);                \
  301.         else                                                            \
  302.             toggle = pokey[chip].output[ch] == !P17(chip);                \
  303.     }                                                                    \
  304.     if( toggle )                                                        \
  305.     {                                                                    \
  306.         if( pokey[chip].output[ch] )                                    \
  307.             sum -= pokey[chip].volume[ch];                                \
  308.         else                                                            \
  309.             sum += pokey[chip].volume[ch];                                \
  310.         pokey[chip].output[ch] ^= 1;                                    \
  311.     }                                                                    \
  312.     /* is this a filtering channel (3/4) and is the filter active? */    \
  313.     if( pokey[chip].AUDCTL & ((CH1_FILTER|CH2_FILTER) & (0x10 >> ch)) ) \
  314.     {                                                                   \
  315.         if( pokey[chip].output[ch-2] )                                    \
  316.         {                                                               \
  317.             pokey[chip].output[ch-2] = 0;                                \
  318.             sum -= pokey[chip].volume[ch-2];                            \
  319.         }                                                               \
  320.     }                                                                   \
  321.  
  322. #endif
  323.  
  324. #define PROCESS_SAMPLE(chip)                                            \
  325.     ADJUST_EVENT(chip);                                                 \
  326.     /* adjust the sample position */                                    \
  327.     pokey[chip].samplepos_fract += pokey[chip].samplerate_24_8;         \
  328.     if( pokey[chip].samplepos_fract & 0xffffff00 )                        \
  329.     {                                                                    \
  330.         pokey[chip].samplepos_whole += pokey[chip].samplepos_fract>>8;    \
  331.         pokey[chip].samplepos_fract &= 0x000000ff;                        \
  332.     }                                                                    \
  333.     /* store sum of output signals into the buffer */                    \
  334.     *buffer++ = (sum > 65535) ? 0x7fff : sum - 0x8000;                    \
  335.     length--
  336.  
  337. #if HEAVY_MACRO_USAGE
  338.  
  339. /*
  340.  * This version of PROCESS_POKEY repeats the search for the minimum
  341.  * event value without using an index to the channel. That way the
  342.  * PROCESS_CHANNEL macros can be called with fixed values and expand
  343.  * to much more efficient code
  344.  */
  345.  
  346. #define PROCESS_POKEY(chip)                                             \
  347.     UINT32 sum = 0;                                                     \
  348.     if( pokey[chip].output[CHAN1] )                                     \
  349.         sum += pokey[chip].volume[CHAN1];                                \
  350.     if( pokey[chip].output[CHAN2] )                                     \
  351.         sum += pokey[chip].volume[CHAN2];                                \
  352.     if( pokey[chip].output[CHAN3] )                                     \
  353.         sum += pokey[chip].volume[CHAN3];                                \
  354.     if( pokey[chip].output[CHAN4] )                                     \
  355.         sum += pokey[chip].volume[CHAN4];                                \
  356.     while( length > 0 )                                                 \
  357.     {                                                                    \
  358.         if( pokey[chip].counter[CHAN1] < pokey[chip].samplepos_whole )    \
  359.         {                                                                \
  360.             if( pokey[chip].counter[CHAN2] <  pokey[chip].counter[CHAN1] ) \
  361.             {                                                            \
  362.                 if( pokey[chip].counter[CHAN3] <  pokey[chip].counter[CHAN2] ) \
  363.                 {                                                        \
  364.                     if( pokey[chip].counter[CHAN4] < pokey[chip].counter[CHAN3] ) \
  365.                     {                                                    \
  366.                         UINT32 event = pokey[chip].counter[CHAN4];        \
  367.                         PROCESS_CHANNEL(chip,CHAN4);                    \
  368.                     }                                                    \
  369.                     else                                                \
  370.                     {                                                    \
  371.                         UINT32 event = pokey[chip].counter[CHAN3];        \
  372.                         PROCESS_CHANNEL(chip,CHAN3);                    \
  373.                     }                                                    \
  374.                 }                                                        \
  375.                 else                                                    \
  376.                 if( pokey[chip].counter[CHAN4] < pokey[chip].counter[CHAN2] )  \
  377.                 {                                                        \
  378.                     UINT32 event = pokey[chip].counter[CHAN4];            \
  379.                     PROCESS_CHANNEL(chip,CHAN4);                        \
  380.                 }                                                        \
  381.                 else                                                    \
  382.                 {                                                        \
  383.                     UINT32 event = pokey[chip].counter[CHAN2];            \
  384.                     PROCESS_CHANNEL(chip,CHAN2);                        \
  385.                 }                                                        \
  386.             }                                                           \
  387.             else                                                        \
  388.             if( pokey[chip].counter[CHAN3] < pokey[chip].counter[CHAN1] ) \
  389.             {                                                            \
  390.                 if( pokey[chip].counter[CHAN4] < pokey[chip].counter[CHAN3] ) \
  391.                 {                                                        \
  392.                     UINT32 event = pokey[chip].counter[CHAN4];            \
  393.                     PROCESS_CHANNEL(chip,CHAN4);                        \
  394.                 }                                                        \
  395.                 else                                                    \
  396.                 {                                                        \
  397.                     UINT32 event = pokey[chip].counter[CHAN3];            \
  398.                     PROCESS_CHANNEL(chip,CHAN3);                        \
  399.                 }                                                        \
  400.             }                                                           \
  401.             else                                                        \
  402.             if( pokey[chip].counter[CHAN4] < pokey[chip].counter[CHAN1] ) \
  403.             {                                                            \
  404.                 UINT32 event = pokey[chip].counter[CHAN4];                \
  405.                 PROCESS_CHANNEL(chip,CHAN4);                            \
  406.             }                                                            \
  407.             else                                                        \
  408.             {                                                            \
  409.                 UINT32 event = pokey[chip].counter[CHAN1];                \
  410.                 PROCESS_CHANNEL(chip,CHAN1);                            \
  411.             }                                                            \
  412.         }                                                                \
  413.         else                                                            \
  414.         if( pokey[chip].counter[CHAN2] < pokey[chip].samplepos_whole )    \
  415.         {                                                                \
  416.             if( pokey[chip].counter[CHAN3] < pokey[chip].counter[CHAN2] ) \
  417.             {                                                            \
  418.                 if( pokey[chip].counter[CHAN4] < pokey[chip].counter[CHAN3] ) \
  419.                 {                                                        \
  420.                     UINT32 event = pokey[chip].counter[CHAN4];            \
  421.                     PROCESS_CHANNEL(chip,CHAN4);                        \
  422.                 }                                                        \
  423.                 else                                                    \
  424.                 {                                                        \
  425.                     UINT32 event = pokey[chip].counter[CHAN3];            \
  426.                     PROCESS_CHANNEL(chip,CHAN3);                        \
  427.                 }                                                        \
  428.             }                                                            \
  429.             else                                                        \
  430.             if( pokey[chip].counter[CHAN4] < pokey[chip].counter[CHAN2] ) \
  431.             {                                                            \
  432.                 UINT32 event = pokey[chip].counter[CHAN4];                \
  433.                 PROCESS_CHANNEL(chip,CHAN4);                            \
  434.             }                                                            \
  435.             else                                                        \
  436.             {                                                            \
  437.                 UINT32 event = pokey[chip].counter[CHAN2];                \
  438.                 PROCESS_CHANNEL(chip,CHAN2);                            \
  439.             }                                                            \
  440.         }                                                                \
  441.         else                                                            \
  442.         if( pokey[chip].counter[CHAN3] < pokey[chip].samplepos_whole )    \
  443.         {                                                               \
  444.             if( pokey[chip].counter[CHAN4] < pokey[chip].counter[CHAN3] ) \
  445.             {                                                            \
  446.                 UINT32 event = pokey[chip].counter[CHAN4];                \
  447.                 PROCESS_CHANNEL(chip,CHAN4);                            \
  448.             }                                                            \
  449.             else                                                        \
  450.             {                                                            \
  451.                 UINT32 event = pokey[chip].counter[CHAN3];                \
  452.                 PROCESS_CHANNEL(chip,CHAN3);                            \
  453.             }                                                            \
  454.         }                                                                \
  455.         else                                                            \
  456.         if( pokey[chip].counter[CHAN4] < pokey[chip].samplepos_whole )    \
  457.         {                                                                \
  458.             UINT32 event = pokey[chip].counter[CHAN4];                    \
  459.             PROCESS_CHANNEL(chip,CHAN4);                                \
  460.         }                                                               \
  461.         else                                                            \
  462.         {                                                                \
  463.             UINT32 event = pokey[chip].samplepos_whole;                 \
  464.             PROCESS_SAMPLE(chip);                                        \
  465.         }                                                                \
  466.     }                                                                    \
  467.     timer_reset(pokey[chip].rtimer, TIME_NEVER)
  468.  
  469. void pokey0_update(int param, INT16 *buffer, int length) { PROCESS_POKEY(0); }
  470. void pokey1_update(int param, INT16 *buffer, int length) { PROCESS_POKEY(1); }
  471. void pokey2_update(int param, INT16 *buffer, int length) { PROCESS_POKEY(2); }
  472. void pokey3_update(int param, INT16 *buffer, int length) { PROCESS_POKEY(3); }
  473. void (*update[MAXPOKEYS])(int,INT16*,int) =
  474.     { pokey0_update,pokey1_update,pokey2_update,pokey3_update };
  475.  
  476. #else   /* no HEAVY_MACRO_USAGE */
  477. /*
  478.  * And this version of PROCESS_POKEY uses event and channel variables
  479.  * so that the PROCESS_CHANNEL macro needs to index memory at runtime.
  480.  */
  481.  
  482. #define PROCESS_POKEY(chip)                                             \
  483.     UINT32 sum = 0;                                                     \
  484.     if( pokey[chip].output[CHAN1] )                                     \
  485.         sum += pokey[chip].volume[CHAN1];                                \
  486.     if( pokey[chip].output[CHAN2] )                                     \
  487.         sum += pokey[chip].volume[CHAN2];                                \
  488.     if( pokey[chip].output[CHAN3] )                                     \
  489.         sum += pokey[chip].volume[CHAN3];                                \
  490.     if( pokey[chip].output[CHAN4] )                                     \
  491.         sum += pokey[chip].volume[CHAN4];                               \
  492.     while( length > 0 )                                                 \
  493.     {                                                                    \
  494.         UINT32 event = pokey[chip].samplepos_whole;                     \
  495.         UINT32 channel = SAMPLE;                                        \
  496.         if( pokey[chip].counter[CHAN1] < event )                        \
  497.         {                                                               \
  498.             event = pokey[chip].counter[CHAN1];                         \
  499.             channel = CHAN1;                                            \
  500.         }                                                                \
  501.         if( pokey[chip].counter[CHAN2] < event )                        \
  502.         {                                                               \
  503.             event = pokey[chip].counter[CHAN2];                         \
  504.             channel = CHAN2;                                            \
  505.         }                                                               \
  506.         if( pokey[chip].counter[CHAN3] < event )                        \
  507.         {                                                               \
  508.             event = pokey[chip].counter[CHAN3];                         \
  509.             channel = CHAN3;                                            \
  510.         }                                                               \
  511.         if( pokey[chip].counter[CHAN4] < event )                        \
  512.         {                                                               \
  513.             event = pokey[chip].counter[CHAN4];                         \
  514.             channel = CHAN4;                                            \
  515.         }                                                               \
  516.         if( channel == SAMPLE )                                         \
  517.         {                                                                \
  518.             PROCESS_SAMPLE(chip);                                       \
  519.         }                                                               \
  520.         else                                                            \
  521.         {                                                                \
  522.             PROCESS_CHANNEL(chip,channel);                                \
  523.         }                                                                \
  524.     }                                                                    \
  525.     timer_reset(pokey[chip].rtimer, TIME_NEVER)
  526.  
  527. void pokey_update(int param, INT16 *buffer, int length) { PROCESS_POKEY(param); }
  528. void (*update[MAXPOKEYS])(int,INT16*,int) =
  529.     { pokey_update,pokey_update,pokey_update,pokey_update };
  530.  
  531. #endif
  532.  
  533. void pokey_sh_update(void)
  534. {
  535.     int chip;
  536.  
  537.     for( chip = 0; chip < intf.num; chip++ )
  538.         stream_update(pokey[chip].channel, 0);
  539. }
  540.  
  541. static void poly_init(UINT8 *poly, int size, int left, int right, int add)
  542. {
  543.     int mask = (1 << size) - 1;
  544.     int i, x = 0;
  545.  
  546.     LOG_POLY(("poly %d\n", size));
  547.     for( i = 0; i < mask; i++ )
  548.     {
  549.         *poly++ = x & 1;
  550.         LOG_POLY(("%05x: %d\n", x, x&1));
  551.         /* calculate next bit */
  552.         x = ((x << left) + (x >> right) + add) & mask;
  553.     }
  554. }
  555.  
  556. static void rand_init(UINT8 *rng, int size, int left, int right, int add)
  557. {
  558.     int mask = (1 << size) - 1;
  559.     int i, x = 0;
  560.  
  561.     LOG_RAND(("rand %d\n", size));
  562.     for( i = 0; i < mask; i++ )
  563.     {
  564.         *rng = x >> (size - 8);   /* use the upper 8 bits */
  565.         LOG_RAND(("%05x: %02x\n", x, *rng));
  566.         rng++;
  567.         /* calculate next bit */
  568.         x = ((x << left) + (x >> right) + add) & mask;
  569.     }
  570. }
  571.  
  572. int pokey_sh_start(const struct MachineSound *msound)
  573. {
  574.     int chip;
  575.  
  576.     memcpy(&intf, msound->sound_interface, sizeof(struct POKEYinterface));
  577.  
  578.     poly9 = malloc(0x1ff+1);
  579.     rand9 = malloc(0x1ff+1);
  580.     poly17 = malloc(0x1ffff+1);
  581.     rand17 = malloc(0x1ffff+1);
  582.     if( !poly9 || !rand9 || !poly17 || !rand17 )
  583.     {
  584.         pokey_sh_stop();    /* free any allocated memory again */
  585.         return 1;
  586.     }
  587.  
  588.     /* initialize the poly counters */
  589.     poly_init(poly4,   4, 3, 1, 0x00004);
  590.     poly_init(poly5,   5, 3, 2, 0x00008);
  591.     poly_init(poly9,   9, 2, 7, 0x00080);
  592.     poly_init(poly17, 17, 7,10, 0x18000);
  593.  
  594.     /* initialize the random arrays */
  595.     rand_init(rand9,   9, 2, 7, 0x00080);
  596.     rand_init(rand17, 17, 7,10, 0x18000);
  597.  
  598.     for( chip = 0; chip < intf.num; chip++ )
  599.     {
  600.         struct POKEYregisters *p = &pokey[chip];
  601.         char name[40];
  602.  
  603.         memset(p, 0, sizeof(struct POKEYregisters));
  604.  
  605.         p->samplerate_24_8 = (Machine->sample_rate) ? (intf.baseclock << 8) / Machine->sample_rate : 1;
  606.         p->divisor[CHAN1] = 4;
  607.         p->divisor[CHAN2] = 4;
  608.         p->divisor[CHAN3] = 4;
  609.         p->divisor[CHAN4] = 4;
  610.         p->clockmult = DIV_64;
  611.         p->KBCODE = 0x09;         /* Atari 800 'no key' */
  612.         p->SKCTL = SK_RESET;     /* let the RNG run after reset */
  613.         p->rtimer = timer_set(TIME_NEVER, chip, NULL);
  614.  
  615.         p->pot_r[0] = intf.pot0_r[chip];
  616.         p->pot_r[1] = intf.pot1_r[chip];
  617.         p->pot_r[2] = intf.pot2_r[chip];
  618.         p->pot_r[3] = intf.pot3_r[chip];
  619.         p->pot_r[4] = intf.pot4_r[chip];
  620.         p->pot_r[5] = intf.pot5_r[chip];
  621.         p->pot_r[6] = intf.pot6_r[chip];
  622.         p->pot_r[7] = intf.pot7_r[chip];
  623.         p->allpot_r = intf.allpot_r[chip];
  624.         p->serin_r = intf.serin_r[chip];
  625.         p->serout_w = intf.serout_w[chip];
  626.         p->interrupt_cb = intf.interrupt_cb[chip];
  627.  
  628.         sprintf(name, "Pokey #%d", chip);
  629.         p->channel = stream_init(name, intf.mixing_level[chip], Machine->sample_rate, chip, update[chip]);
  630.  
  631.         if( p->channel == -1 )
  632.         {
  633.             logerror("failed to initialize sound channel\n");
  634.             return 1;
  635.         }
  636.     }
  637.  
  638.     return 0;
  639. }
  640.  
  641. void pokey_sh_stop (void)
  642. {
  643.     if( rand17 ) free(rand17);
  644.     rand17 = NULL;
  645.     if( poly17 ) free(poly17);
  646.     poly17 = NULL;
  647.     if( rand9 )  free(rand9);
  648.     rand9 = NULL;
  649.     if( poly9 )  free(poly9);
  650.     poly9 = NULL;
  651. }
  652.  
  653. static void pokey_timer_expire(int param)
  654. {
  655.     int chip = param >> 3;
  656.     int timers = param & 7;
  657.     struct POKEYregisters *p = &pokey[chip];
  658.  
  659.     LOG_TIMER(("POKEY #%d timer %d with IRQEN $%02x\n", chip, param, p->IRQEN));
  660.  
  661.     /* check if some of the requested timer interrupts are enabled */
  662.     timers &= p->IRQEN;
  663.  
  664.     if( timers )
  665.     {
  666.         /* set the enabled timer irq status bits */
  667.         p->IRQST |= timers;
  668.         /* call back an application supplied function to handle the interrupt */
  669.         if( p->interrupt_cb )
  670.             (*p->interrupt_cb)(timers);
  671.     }
  672. }
  673.  
  674. #if VERBOSE_SOUND
  675. static char *audc2str(int val)
  676. {
  677.     static char buff[80];
  678.     if( val & NOTPOLY5 )
  679.     {
  680.         if( val & PURE )
  681.             strcpy(buff,"pure");
  682.         else
  683.         if( val & POLY4 )
  684.             strcpy(buff,"poly4");
  685.         else
  686.             strcpy(buff,"poly9/17");
  687.     }
  688.     else
  689.     {
  690.         if( val & PURE )
  691.             strcpy(buff,"poly5");
  692.         else
  693.         if( val & POLY4 )
  694.             strcpy(buff,"poly4+poly5");
  695.         else
  696.             strcpy(buff,"poly9/17+poly5");
  697.     }
  698.     return buff;
  699. }
  700.  
  701. static char *audctl2str(int val)
  702. {
  703.     static char buff[80];
  704.     if( val & POLY9 )
  705.         strcpy(buff,"poly9");
  706.     else
  707.         strcpy(buff,"poly17");
  708.     if( val & CH1_HICLK )
  709.         strcat(buff,"+ch1hi");
  710.     if( val & CH3_HICLK )
  711.         strcat(buff,"+ch3hi");
  712.     if( val & CH12_JOINED )
  713.         strcat(buff,"+ch1/2");
  714.     if( val & CH34_JOINED )
  715.         strcat(buff,"+ch3/4");
  716.     if( val & CH1_FILTER )
  717.         strcat(buff,"+ch1filter");
  718.     if( val & CH2_FILTER )
  719.         strcat(buff,"+ch2filter");
  720.     if( val & CLK_15KHZ )
  721.         strcat(buff,"+clk15");
  722.     return buff;
  723. }
  724. #endif
  725.  
  726. static void pokey_serin_ready(int chip)
  727. {
  728.     struct POKEYregisters *p = &pokey[chip];
  729.     if( p->IRQEN & IRQ_SERIN )
  730.     {
  731.         /* set the enabled timer irq status bits */
  732.         p->IRQST |= IRQ_SERIN;
  733.         /* call back an application supplied function to handle the interrupt */
  734.         if( p->interrupt_cb )
  735.             (*p->interrupt_cb)(IRQ_SERIN);
  736.     }
  737. }
  738.  
  739. static void pokey_serout_ready(int chip)
  740. {
  741.     struct POKEYregisters *p = &pokey[chip];
  742.     if( p->IRQEN & IRQ_SEROR )
  743.     {
  744.         p->IRQST |= IRQ_SEROR;
  745.         if( p->interrupt_cb )
  746.             (*p->interrupt_cb)(IRQ_SEROR);
  747.     }
  748. }
  749.  
  750. static void pokey_serout_complete(int chip)
  751. {
  752.     struct POKEYregisters *p = &pokey[chip];
  753.     if( p->IRQEN & IRQ_SEROC )
  754.     {
  755.         p->IRQST |= IRQ_SEROC;
  756.         if( p->interrupt_cb )
  757.             (*p->interrupt_cb)(IRQ_SEROC);
  758.     }
  759. }
  760.  
  761. static void pokey_pot_trigger(int param)
  762. {
  763.     int chip = param >> 3;
  764.     int pot = param & 7;
  765.     struct POKEYregisters *p = &pokey[chip];
  766.  
  767.     LOG(("POKEY #%d POT%d triggers after %dus\n", chip, pot, (int)(1000000ul*timer_timeelapsed(p->ptimer[pot]))));
  768.     p->ptimer[pot] = NULL;
  769.     p->ALLPOT &= ~(1 << pot);    /* set the enabled timer irq status bits */
  770. }
  771.  
  772. /* A/D conversion time:
  773.  * In normal, slow mode (SKCTL bit SK_PADDLE is clear) the conversion
  774.  * takes N scanlines, where N is the paddle value. A single scanline
  775.  * takes approximately 64us to finish (1.78979MHz clock).
  776.  * In quick mode (SK_PADDLE set) the conversion is done very fast
  777.  * (takes two scalines) but the result is not as accurate.
  778.  */
  779. #define AD_TIME (double)(((p->SKCTL & SK_PADDLE) ? 64.0*2/228 : 64.0) * FREQ_17_EXACT / intf.baseclock)
  780.  
  781. static void pokey_potgo(int chip)
  782. {
  783.     struct POKEYregisters *p = &pokey[chip];
  784.     int pot;
  785.  
  786.     LOG(("POKEY #%d pokey_potgo\n", chip));
  787.  
  788.     p->ALLPOT = 0xff;
  789.  
  790.     for( pot = 0; pot < 8; pot++ )
  791.     {
  792.         if( p->ptimer[pot] )
  793.         {
  794.             timer_remove(p->ptimer[pot]);
  795.             p->ptimer[pot] = NULL;
  796.             p->POTx[pot] = 0xff;
  797.         }
  798.         if( p->pot_r[pot] )
  799.         {
  800.             int r = (*p->pot_r[pot])(pot);
  801.             LOG(("POKEY #%d pot_r(%d) returned $%02x\n", chip, pot, r));
  802.             if( r != -1 )
  803.             {
  804.                 if (r > 228)
  805.                     r = 228;
  806.                 /* final value */
  807.                 p->POTx[pot] = r;
  808.                 p->ptimer[pot] = timer_set(TIME_IN_USEC(r * AD_TIME), (chip<<3)|pot, pokey_pot_trigger);
  809.             }
  810.         }
  811.     }
  812. }
  813.  
  814. int pokey_register_r(int chip, int offs)
  815. {
  816.     struct POKEYregisters *p = &pokey[chip];
  817.     int data = 0, pot;
  818.  
  819. #ifdef MAME_DEBUG
  820.     if( chip >= intf.num )
  821.     {
  822.         logerror("POKEY #%d is >= number of Pokeys!\n", chip);
  823.         return data;
  824.     }
  825. #endif
  826.  
  827.     switch (offs & 15)
  828.     {
  829.     case POT0_C: case POT1_C: case POT2_C: case POT3_C:
  830.     case POT4_C: case POT5_C: case POT6_C: case POT7_C:
  831.         pot = offs & 7;
  832.         if( p->pot_r[pot] )
  833.         {
  834.             /*
  835.              * If the conversion is not yet finished (ptimer running),
  836.              * get the current value by the linear interpolation of
  837.              * the final value using the elapsed time.
  838.              */
  839.             if( p->ALLPOT & (1 << pot) )
  840.             {
  841.                 data = (UINT8)(timer_timeelapsed(p->ptimer[pot]) / AD_TIME);
  842.                 LOG(("POKEY #%d read POT%d (interpolated) $%02x\n", chip, pot, data));
  843.             }
  844.             else
  845.             {
  846.                 data = p->POTx[pot];
  847.                 LOG(("POKEY #%d read POT%d (final value)  $%02x\n", chip, pot, data));
  848.             }
  849.         }
  850.         else
  851.         logerror("PC %04x: warning - read p[chip] #%d POT%d\n", cpu_get_pc(), chip, pot);
  852.         break;
  853.  
  854.     case ALLPOT_C:
  855.         if( p->allpot_r )
  856.         {
  857.             data = (*p->allpot_r)(offs);
  858.             LOG(("POKEY #%d ALLPOT callback $%02x\n", chip, data));
  859.         }
  860.         else
  861.         {
  862.             data = p->ALLPOT;
  863.             LOG(("POKEY #%d ALLPOT internal $%02x\n", chip, data));
  864.         }
  865.         break;
  866.  
  867.     case KBCODE_C:
  868.         data = p->KBCODE;
  869.         break;
  870.  
  871.     case RANDOM_C:
  872.         /****************************************************************
  873.          * If the 2 least significant bits of SKCTL are 0, the random
  874.          * number generator is disabled (SKRESET). Thanks to Eric Smith
  875.          * for pointing out this critical bit of info! If the random
  876.          * number generator is enabled, get a new random number. Take
  877.          * the time gone since the last read into account and read the
  878.          * new value from an appropriate offset in the rand17 table.
  879.          ****************************************************************/
  880.         if( p->SKCTL & SK_RESET )
  881.         {
  882.             UINT32 adjust = (UINT32)(timer_timeelapsed(p->rtimer) * intf.baseclock);
  883.             p->r9 = (p->r9 + adjust) % 0x001ff;
  884.             p->r17 = (p->r17 + adjust) % 0x1ffff;
  885.             if( p->AUDCTL & POLY9 )
  886.             {
  887.                 p->RANDOM = rand9[p->r9];
  888.                 LOG_RAND(("POKEY #%d adjust %u rand9[$%05x]: $%02x\n", chip, adjust, p->r9, p->RANDOM));
  889.             }
  890.             else
  891.             {
  892.                 p->RANDOM = rand17[p->r17];
  893.                 LOG_RAND(("POKEY #%d adjust %u rand17[$%05x]: $%02x\n", chip, adjust, p->r17, p->RANDOM));
  894.             }
  895.         }
  896.         else
  897.         {
  898.             LOG_RAND(("POKEY #%d rand17 freezed (SKCTL): $%02x\n", chip, p->RANDOM));
  899.         }
  900.         timer_reset(p->rtimer, TIME_NEVER);
  901.         data = p->RANDOM;
  902.         break;
  903.  
  904.     case SERIN_C:
  905.         if( p->serin_r )
  906.             p->SERIN = (*p->serin_r)(offs);
  907.         data = p->SERIN;
  908.         LOG(("POKEY #%d SERIN  $%02x\n", chip, data));
  909.         break;
  910.  
  911.     case IRQST_C:
  912.         /* IRQST is an active low input port; we keep it active high */
  913.         /* internally to ease the (un-)masking of bits */
  914.         data = p->IRQST ^ 0xff;
  915.         LOG(("POKEY #%d IRQST  $%02x\n", chip, data));
  916.         break;
  917.  
  918.     case SKSTAT_C:
  919.         /* SKSTAT is also an active low input port */
  920.         data = p->SKSTAT ^ 0xff;
  921.         LOG(("POKEY #%d SKSTAT $%02x\n", chip, data));
  922.         break;
  923.  
  924.     default:
  925.         LOG(("POKEY #%d register $%02x\n", chip, offs));
  926.         break;
  927.     }
  928.     return data;
  929. }
  930.  
  931. READ_HANDLER( pokey1_r )
  932. {
  933.     return pokey_register_r(0, offset);
  934. }
  935.  
  936. READ_HANDLER( pokey2_r )
  937. {
  938.     return pokey_register_r(1, offset);
  939. }
  940.  
  941. READ_HANDLER( pokey3_r )
  942. {
  943.     return pokey_register_r(2, offset);
  944. }
  945.  
  946. READ_HANDLER( pokey4_r )
  947. {
  948.     return pokey_register_r(3, offset);
  949. }
  950.  
  951. READ_HANDLER( quad_pokey_r )
  952. {
  953.     int pokey_num = (offset >> 3) & ~0x04;
  954.     int control = (offset & 0x20) >> 2;
  955.     int pokey_reg = (offset % 8) | control;
  956.  
  957.     return pokey_register_r(pokey_num, pokey_reg);
  958. }
  959.  
  960.  
  961. void pokey_register_w(int chip, int offs, int data)
  962. {
  963.     struct POKEYregisters *p = &pokey[chip];
  964.     int ch_mask = 0, new_val;
  965.  
  966. #ifdef MAME_DEBUG
  967.     if( chip >= intf.num )
  968.     {
  969.         logerror("POKEY #%d is >= number of Pokeys!\n", chip);
  970.         return;
  971.     }
  972. #endif
  973.     stream_update(p->channel, 0);
  974.  
  975.     /* determine which address was changed */
  976.     switch (offs & 15)
  977.     {
  978.     case AUDF1_C:
  979.         if( data == p->AUDF[CHAN1] )
  980.             return;
  981.         LOG_SOUND(("POKEY #%d AUDF1  $%02x\n", chip, data));
  982.         p->AUDF[CHAN1] = data;
  983.         ch_mask = 1 << CHAN1;
  984.         if( p->AUDCTL & CH12_JOINED )        /* if ch 1&2 tied together */
  985.             ch_mask |= 1 << CHAN2;    /* then also change on ch2 */
  986.         break;
  987.  
  988.     case AUDC1_C:
  989.         if( data == p->AUDC[CHAN1] )
  990.             return;
  991.         LOG_SOUND(("POKEY #%d AUDC1  $%02x (%s)\n", chip, data, audc2str(data)));
  992.         p->AUDC[CHAN1] = data;
  993.         ch_mask = 1 << CHAN1;
  994.         break;
  995.  
  996.     case AUDF2_C:
  997.         if( data == p->AUDF[CHAN2] )
  998.             return;
  999.         LOG_SOUND(("POKEY #%d AUDF2  $%02x\n", chip, data));
  1000.         p->AUDF[CHAN2] = data;
  1001.         ch_mask = 1 << CHAN2;
  1002.         break;
  1003.  
  1004.     case AUDC2_C:
  1005.         if( data == p->AUDC[CHAN2] )
  1006.             return;
  1007.         LOG_SOUND(("POKEY #%d AUDC2  $%02x (%s)\n", chip, data, audc2str(data)));
  1008.         p->AUDC[CHAN2] = data;
  1009.         ch_mask = 1 << CHAN2;
  1010.         break;
  1011.  
  1012.     case AUDF3_C:
  1013.         if( data == p->AUDF[CHAN3] )
  1014.             return;
  1015.         LOG_SOUND(("POKEY #%d AUDF3  $%02x\n", chip, data));
  1016.         p->AUDF[CHAN3] = data;
  1017.         ch_mask = 1 << CHAN3;
  1018.  
  1019.         if( p->AUDCTL & CH34_JOINED )    /* if ch 3&4 tied together */
  1020.             ch_mask |= 1 << CHAN4;  /* then also change on ch4 */
  1021.         break;
  1022.  
  1023.     case AUDC3_C:
  1024.         if( data == p->AUDC[CHAN3] )
  1025.             return;
  1026.         LOG_SOUND(("POKEY #%d AUDC3  $%02x (%s)\n", chip, data, audc2str(data)));
  1027.         p->AUDC[CHAN3] = data;
  1028.         ch_mask = 1 << CHAN3;
  1029.         break;
  1030.  
  1031.     case AUDF4_C:
  1032.         if( data == p->AUDF[CHAN4] )
  1033.             return;
  1034.         LOG_SOUND(("POKEY #%d AUDF4  $%02x\n", chip, data));
  1035.         p->AUDF[CHAN4] = data;
  1036.         ch_mask = 1 << CHAN4;
  1037.         break;
  1038.  
  1039.     case AUDC4_C:
  1040.         if( data == p->AUDC[CHAN4] )
  1041.             return;
  1042.         LOG_SOUND(("POKEY #%d AUDC4  $%02x (%s)\n", chip, data, audc2str(data)));
  1043.         p->AUDC[CHAN4] = data;
  1044.         ch_mask = 1 << CHAN4;
  1045.         break;
  1046.  
  1047.     case AUDCTL_C:
  1048.         if( data == p->AUDCTL )
  1049.             return;
  1050.         LOG_SOUND(("POKEY #%d AUDCTL $%02x (%s)\n", chip, data, audctl2str(data)));
  1051.         p->AUDCTL = data;
  1052.         ch_mask = 15;       /* all channels */
  1053.         /* determine the base multiplier for the 'div by n' calculations */
  1054.         p->clockmult = (p->AUDCTL & CLK_15KHZ) ? DIV_15 : DIV_64;
  1055.         break;
  1056.  
  1057.     case STIMER_C:
  1058.         /* first remove any existing timers */
  1059.         LOG_TIMER(("POKEY #%d STIMER $%02x\n", chip, data));
  1060.         if( p->timer[TIMER1] )
  1061.             timer_remove(p->timer[TIMER1]);
  1062.         if( p->timer[TIMER2] )
  1063.             timer_remove(p->timer[TIMER2]);
  1064.         if( p->timer[TIMER4] )
  1065.             timer_remove(p->timer[TIMER4]);
  1066.         p->timer[TIMER1] = NULL;
  1067.         p->timer[TIMER2] = NULL;
  1068.         p->timer[TIMER4] = NULL;
  1069.  
  1070.         /* reset all counters to zero (side effect) */
  1071.         p->polyadjust = 0;
  1072.         p->counter[CHAN1] = 0;
  1073.         p->counter[CHAN2] = 0;
  1074.         p->counter[CHAN3] = 0;
  1075.         p->counter[CHAN4] = 0;
  1076.  
  1077.         /* joined chan#1 and chan#2 ? */
  1078.         if( p->AUDCTL & CH12_JOINED )
  1079.         {
  1080.             if( p->divisor[CHAN2] > 4 )
  1081.             {
  1082.                 LOG_TIMER(("POKEY #%d timer1+2 after %d clocks\n", chip, p->divisor[CHAN2]));
  1083.                 /* set timer #1 _and_ #2 event after timer_div clocks of joined CHAN1+CHAN2 */
  1084.                 p->timer[TIMER2] =
  1085.                     timer_pulse(1.0 * p->divisor[CHAN2] / intf.baseclock,
  1086.                         (chip<<3)|IRQ_TIMR2|IRQ_TIMR1, pokey_timer_expire);
  1087.             }
  1088.         }
  1089.         else
  1090.         {
  1091.             if( p->divisor[CHAN1] > 4 )
  1092.             {
  1093.                 LOG_TIMER(("POKEY #%d timer1 after %d clocks\n", chip, p->divisor[CHAN1]));
  1094.                 /* set timer #1 event after timer_div clocks of CHAN1 */
  1095.                 p->timer[TIMER1] =
  1096.                     timer_pulse(1.0 * p->divisor[CHAN1] / intf.baseclock,
  1097.                         (chip<<3)|IRQ_TIMR1, pokey_timer_expire);
  1098.             }
  1099.  
  1100.             if( p->divisor[CHAN2] > 4 )
  1101.             {
  1102.                 LOG_TIMER(("POKEY #%d timer2 after %d clocks\n", chip, p->divisor[CHAN2]));
  1103.                 /* set timer #2 event after timer_div clocks of CHAN2 */
  1104.                 p->timer[TIMER2] =
  1105.                     timer_pulse(1.0 * p->divisor[CHAN2] / intf.baseclock,
  1106.                         (chip<<3)|IRQ_TIMR2, pokey_timer_expire);
  1107.             }
  1108.         }
  1109.  
  1110.         /* Note: p[chip] does not have a timer #3 */
  1111.  
  1112.         if( p->AUDCTL & CH34_JOINED )
  1113.         {
  1114.             /* not sure about this: if audc4 == 0000xxxx don't start timer 4 ? */
  1115.             if( p->AUDC[CHAN4] & 0xf0 )
  1116.             {
  1117.                 if( p->divisor[CHAN4] > 4 )
  1118.                 {
  1119.                     LOG_TIMER(("POKEY #%d timer4 after %d clocks\n", chip, p->divisor[CHAN4]));
  1120.                     /* set timer #4 event after timer_div clocks of CHAN4 */
  1121.                     p->timer[TIMER4] =
  1122.                         timer_pulse(1.0 * p->divisor[CHAN4] / intf.baseclock,
  1123.                             (chip<<3)|IRQ_TIMR4, pokey_timer_expire);
  1124.                 }
  1125.             }
  1126.         }
  1127.         else
  1128.         {
  1129.             if( p->divisor[CHAN4] > 4 )
  1130.             {
  1131.                 LOG_TIMER(("POKEY #%d timer4 after %d clocks\n", chip, p->divisor[CHAN4]));
  1132.                 /* set timer #4 event after timer_div clocks of CHAN4 */
  1133.                 p->timer[TIMER4] =
  1134.                     timer_pulse(1.0 * p->divisor[CHAN4] / intf.baseclock,
  1135.                         (chip<<3)|IRQ_TIMR4, pokey_timer_expire);
  1136.             }
  1137.         }
  1138.         if( p->timer[TIMER1] )
  1139.             timer_enable(p->timer[TIMER1], p->IRQEN & IRQ_TIMR1);
  1140.         if( p->timer[TIMER2] )
  1141.             timer_enable(p->timer[TIMER2], p->IRQEN & IRQ_TIMR2);
  1142.         if( p->timer[TIMER4] )
  1143.             timer_enable(p->timer[TIMER4], p->IRQEN & IRQ_TIMR4);
  1144.         break;
  1145.  
  1146.     case SKREST_C:
  1147.         /* reset SKSTAT */
  1148.         LOG(("POKEY #%d SKREST $%02x\n", chip, data));
  1149.         p->SKSTAT &= ~(SK_FRAME|SK_OVERRUN|SK_KBERR);
  1150.         break;
  1151.  
  1152.     case POTGO_C:
  1153.         LOG(("POKEY #%d POTGO  $%02x\n", chip, data));
  1154.         pokey_potgo(chip);
  1155.         break;
  1156.  
  1157.     case SEROUT_C:
  1158.         LOG(("POKEY #%d SEROUT $%02x\n", chip, data));
  1159.         if (p->serout_w)
  1160.             (*p->serout_w)(offs, data);
  1161.         p->SKSTAT |= SK_SEROUT;
  1162.         /*
  1163.          * These are arbitrary values, tested with some custom boot
  1164.          * loaders from Ballblazer and Escape from Fractalus
  1165.          * The real times are unknown
  1166.          */
  1167.         timer_set(TIME_IN_USEC(200), chip, pokey_serout_ready);
  1168.         /* 10 bits (assumption 1 start, 8 data and 1 stop bit) take how long? */
  1169.         timer_set(TIME_IN_USEC(2000), chip, pokey_serout_complete);
  1170.         break;
  1171.  
  1172.     case IRQEN_C:
  1173.         LOG(("POKEY #%d IRQEN  $%02x\n", chip, data));
  1174.  
  1175.         /* acknowledge one or more IRQST bits ? */
  1176.         if( p->IRQST & ~data )
  1177.         {
  1178.             /* reset IRQST bits that are masked now */
  1179.             p->IRQST &= data;
  1180.         }
  1181.         else
  1182.         {
  1183.             /* enable/disable timers now to avoid unneeded
  1184.                breaking of the CPU cores for masked timers */
  1185.             if( p->timer[TIMER1] && ((p->IRQEN^data) & IRQ_TIMR1) )
  1186.                 timer_enable(p->timer[TIMER1], data & IRQ_TIMR1);
  1187.             if( p->timer[TIMER2] && ((p->IRQEN^data) & IRQ_TIMR2) )
  1188.                 timer_enable(p->timer[TIMER2], data & IRQ_TIMR2);
  1189.             if( p->timer[TIMER4] && ((p->IRQEN^data) & IRQ_TIMR4) )
  1190.                 timer_enable(p->timer[TIMER4], data & IRQ_TIMR4);
  1191.         }
  1192.         /* store irq enable */
  1193.         p->IRQEN = data;
  1194.         break;
  1195.  
  1196.     case SKCTL_C:
  1197.         if( data == p->SKCTL )
  1198.             return;
  1199.         LOG(("POKEY #%d SKCTL  $%02x\n", chip, data));
  1200.         p->SKCTL = data;
  1201.         if( !(data & SK_RESET) )
  1202.         {
  1203.             pokey_register_w(chip, IRQEN_C,  0);
  1204.             pokey_register_w(chip, SKREST_C, 0);
  1205.         }
  1206.         break;
  1207.     }
  1208.  
  1209.     /************************************************************
  1210.      * As defined in the manual, the exact counter values are
  1211.      * different depending on the frequency and resolution:
  1212.      *      64 kHz or 15 kHz - AUDF + 1
  1213.      *      1.79 MHz, 8-bit  - AUDF + 4
  1214.      *      1.79 MHz, 16-bit - AUDF[CHAN1]+256*AUDF[CHAN2] + 7
  1215.      ************************************************************/
  1216.  
  1217.     /* only reset the channels that have changed */
  1218.  
  1219.     if( ch_mask & (1 << CHAN1) )
  1220.     {
  1221.         /* process channel 1 frequency */
  1222.         if( p->AUDCTL & CH1_HICLK )
  1223.             new_val = p->AUDF[CHAN1] + DIVADD_HICLK;
  1224.         else
  1225.             new_val = (p->AUDF[CHAN1] + DIVADD_LOCLK) * p->clockmult;
  1226.  
  1227.         LOG_SOUND(("POKEY #%d chan1 %d\n", chip, new_val));
  1228.  
  1229.         p->volume[CHAN1] = (p->AUDC[CHAN1] & VOLUME_MASK) * POKEY_DEFAULT_GAIN;
  1230.         p->divisor[CHAN1] = new_val;
  1231.         if( new_val < p->counter[CHAN1] )
  1232.             p->counter[CHAN1] = new_val;
  1233.         if( p->interrupt_cb && p->timer[TIMER1] )
  1234.             timer_reset(p->timer[TIMER1], 1.0 * new_val / intf.baseclock);
  1235.         p->audible[CHAN1] = !(
  1236.             (p->AUDC[CHAN1] & VOLUME_ONLY) ||
  1237.             (p->AUDC[CHAN1] & VOLUME_MASK) == 0 ||
  1238.             ((p->AUDC[CHAN1] & PURE) && new_val < (p->samplerate_24_8 >> 8)));
  1239.         if( !p->audible[CHAN1] )
  1240.         {
  1241.             p->output[CHAN1] = 1;
  1242.             p->counter[CHAN1] = 0x7fffffff;
  1243.             /* 50% duty cycle should result in half volume */
  1244.             p->volume[CHAN1] >>= 1;
  1245.         }
  1246.     }
  1247.  
  1248.     if( ch_mask & (1 << CHAN2) )
  1249.     {
  1250.         /* process channel 2 frequency */
  1251.         if( p->AUDCTL & CH12_JOINED )
  1252.         {
  1253.             if( p->AUDCTL & CH1_HICLK )
  1254.                 new_val = p->AUDF[CHAN2] * 256 + p->AUDF[CHAN1] + DIVADD_HICLK_JOINED;
  1255.             else
  1256.                 new_val = (p->AUDF[CHAN2] * 256 + p->AUDF[CHAN1] + DIVADD_LOCLK) * p->clockmult;
  1257.             LOG_SOUND(("POKEY #%d chan1+2 %d\n", chip, new_val));
  1258.         }
  1259.         else
  1260.         {
  1261.             new_val = (p->AUDF[CHAN2] + DIVADD_LOCLK) * p->clockmult;
  1262.             LOG_SOUND(("POKEY #%d chan2 %d\n", chip, new_val));
  1263.         }
  1264.  
  1265.         p->volume[CHAN2] = (p->AUDC[CHAN2] & VOLUME_MASK) * POKEY_DEFAULT_GAIN;
  1266.         p->divisor[CHAN2] = new_val;
  1267.         if( new_val < p->counter[CHAN2] )
  1268.             p->counter[CHAN2] = new_val;
  1269.         if( p->interrupt_cb && p->timer[TIMER2] )
  1270.             timer_reset(p->timer[TIMER2], 1.0 * new_val / intf.baseclock);
  1271.         p->audible[CHAN2] = !(
  1272.             (p->AUDC[CHAN2] & VOLUME_ONLY) ||
  1273.             (p->AUDC[CHAN2] & VOLUME_MASK) == 0 ||
  1274.             ((p->AUDC[CHAN2] & PURE) && new_val < (p->samplerate_24_8 >> 8)));
  1275.         if( !p->audible[CHAN2] )
  1276.         {
  1277.             p->output[CHAN2] = 1;
  1278.             p->counter[CHAN2] = 0x7fffffff;
  1279.             /* 50% duty cycle should result in half volume */
  1280.             p->volume[CHAN2] >>= 1;
  1281.         }
  1282.     }
  1283.  
  1284.     if( ch_mask & (1 << CHAN3) )
  1285.     {
  1286.         /* process channel 3 frequency */
  1287.         if( p->AUDCTL & CH3_HICLK )
  1288.             new_val = p->AUDF[CHAN3] + DIVADD_HICLK;
  1289.         else
  1290.             new_val = (p->AUDF[CHAN3] + DIVADD_LOCLK) * p->clockmult;
  1291.  
  1292.         LOG_SOUND(("POKEY #%d chan3 %d\n", chip, new_val));
  1293.  
  1294.         p->volume[CHAN3] = (p->AUDC[CHAN3] & VOLUME_MASK) * POKEY_DEFAULT_GAIN;
  1295.         p->divisor[CHAN3] = new_val;
  1296.         if( new_val < p->counter[CHAN3] )
  1297.             p->counter[CHAN3] = new_val;
  1298.         /* channel 3 does not have a timer associated */
  1299.         p->audible[CHAN3] = !(
  1300.             (p->AUDC[CHAN3] & VOLUME_ONLY) ||
  1301.             (p->AUDC[CHAN3] & VOLUME_MASK) == 0 ||
  1302.             ((p->AUDC[CHAN3] & PURE) && new_val < (p->samplerate_24_8 >> 8))) ||
  1303.             (p->AUDCTL & CH1_FILTER);
  1304.         if( !p->audible[CHAN3] )
  1305.         {
  1306.             p->output[CHAN3] = 1;
  1307.             p->counter[CHAN3] = 0x7fffffff;
  1308.             /* 50% duty cycle should result in half volume */
  1309.             p->volume[CHAN3] >>= 1;
  1310.         }
  1311.     }
  1312.  
  1313.     if( ch_mask & (1 << CHAN4) )
  1314.     {
  1315.         /* process channel 4 frequency */
  1316.         if( p->AUDCTL & CH34_JOINED )
  1317.         {
  1318.             if( p->AUDCTL & CH3_HICLK )
  1319.                 new_val = p->AUDF[CHAN4] * 256 + p->AUDF[CHAN3] + DIVADD_HICLK_JOINED;
  1320.             else
  1321.                 new_val = (p->AUDF[CHAN4] * 256 + p->AUDF[CHAN3] + DIVADD_LOCLK) * p->clockmult;
  1322.             LOG_SOUND(("POKEY #%d chan3+4 %d\n", chip, new_val));
  1323.         }
  1324.         else
  1325.         {
  1326.             new_val = (p->AUDF[CHAN4] + DIVADD_LOCLK) * p->clockmult;
  1327.             LOG_SOUND(("POKEY #%d chan4 %d\n", chip, new_val));
  1328.         }
  1329.  
  1330.         p->volume[CHAN4] = (p->AUDC[CHAN4] & VOLUME_MASK) * POKEY_DEFAULT_GAIN;
  1331.         p->divisor[CHAN4] = new_val;
  1332.         if( new_val < p->counter[CHAN4] )
  1333.             p->counter[CHAN4] = new_val;
  1334.         if( p->interrupt_cb && p->timer[TIMER4] )
  1335.             timer_reset(p->timer[TIMER4], 1.0 * new_val / intf.baseclock);
  1336.         p->audible[CHAN4] = !(
  1337.             (p->AUDC[CHAN4] & VOLUME_ONLY) ||
  1338.             (p->AUDC[CHAN4] & VOLUME_MASK) == 0 ||
  1339.             ((p->AUDC[CHAN4] & PURE) && new_val < (p->samplerate_24_8 >> 8))) ||
  1340.             (p->AUDCTL & CH2_FILTER);
  1341.         if( !p->audible[CHAN4] )
  1342.         {
  1343.             p->output[CHAN4] = 1;
  1344.             p->counter[CHAN4] = 0x7fffffff;
  1345.             /* 50% duty cycle should result in half volume */
  1346.             p->volume[CHAN4] >>= 1;
  1347.         }
  1348.     }
  1349. }
  1350.  
  1351. WRITE_HANDLER( pokey1_w )
  1352. {
  1353.     pokey_register_w(0,offset,data);
  1354. }
  1355.  
  1356. WRITE_HANDLER( pokey2_w )
  1357. {
  1358.     pokey_register_w(1,offset,data);
  1359. }
  1360.  
  1361. WRITE_HANDLER( pokey3_w )
  1362. {
  1363.     pokey_register_w(2,offset,data);
  1364. }
  1365.  
  1366. WRITE_HANDLER( pokey4_w )
  1367. {
  1368.     pokey_register_w(3,offset,data);
  1369. }
  1370.  
  1371. WRITE_HANDLER( quad_pokey_w )
  1372. {
  1373.     int pokey_num = (offset >> 3) & ~0x04;
  1374.     int control = (offset & 0x20) >> 2;
  1375.     int pokey_reg = (offset % 8) | control;
  1376.  
  1377.     pokey_register_w(pokey_num, pokey_reg, data);
  1378. }
  1379.  
  1380. void pokey1_serin_ready(int after)
  1381. {
  1382.     timer_set(1.0 * after / intf.baseclock, 0, pokey_serin_ready);
  1383. }
  1384.  
  1385. void pokey2_serin_ready(int after)
  1386. {
  1387.     timer_set(1.0 * after / intf.baseclock, 1, pokey_serin_ready);
  1388. }
  1389.  
  1390. void pokey3_serin_ready(int after)
  1391. {
  1392.     timer_set(1.0 * after / intf.baseclock, 2, pokey_serin_ready);
  1393. }
  1394.  
  1395. void pokey4_serin_ready(int after)
  1396. {
  1397.     timer_set(1.0 * after / intf.baseclock, 3, pokey_serin_ready);
  1398. }
  1399.  
  1400. void pokey_break_w(int chip, int shift)
  1401. {
  1402.     struct POKEYregisters *p = &pokey[chip];
  1403.     if( shift )                     /* shift code ? */
  1404.         p->SKSTAT |= SK_SHIFT;
  1405.     else
  1406.         p->SKSTAT &= ~SK_SHIFT;
  1407.     /* check if the break IRQ is enabled */
  1408.     if( p->IRQEN & IRQ_BREAK )
  1409.     {
  1410.         /* set break IRQ status and call back the interrupt handler */
  1411.         p->IRQST |= IRQ_BREAK;
  1412.         if( p->interrupt_cb )
  1413.             (*p->interrupt_cb)(IRQ_BREAK);
  1414.     }
  1415. }
  1416.  
  1417. void pokey1_break_w(int shift)
  1418. {
  1419.     pokey_break_w(0, shift);
  1420. }
  1421.  
  1422. void pokey2_break_w(int shift)
  1423. {
  1424.     pokey_break_w(1, shift);
  1425. }
  1426.  
  1427. void pokey3_break_w(int shift)
  1428. {
  1429.     pokey_break_w(2, shift);
  1430. }
  1431.  
  1432. void pokey4_break_w(int shift)
  1433. {
  1434.     pokey_break_w(3, shift);
  1435. }
  1436.  
  1437. void pokey_kbcode_w(int chip, int kbcode, int make)
  1438. {
  1439.     struct POKEYregisters *p = &pokey[chip];
  1440.     /* make code ? */
  1441.     if( make )
  1442.     {
  1443.         p->KBCODE = kbcode;
  1444.         p->SKSTAT |= SK_KEYBD;
  1445.         if( kbcode & 0x40 )         /* shift code ? */
  1446.             p->SKSTAT |= SK_SHIFT;
  1447.         else
  1448.             p->SKSTAT &= ~SK_SHIFT;
  1449.  
  1450.         if( p->IRQEN & IRQ_KEYBD )
  1451.         {
  1452.             /* last interrupt not acknowledged ? */
  1453.             if( p->IRQST & IRQ_KEYBD )
  1454.                 p->SKSTAT |= SK_KBERR;
  1455.             p->IRQST |= IRQ_KEYBD;
  1456.             if( p->interrupt_cb )
  1457.                 (*p->interrupt_cb)(IRQ_KEYBD);
  1458.         }
  1459.     }
  1460.     else
  1461.     {
  1462.         p->KBCODE = kbcode;
  1463.         p->SKSTAT &= ~SK_KEYBD;
  1464.     }
  1465. }
  1466.  
  1467. void pokey1_kbcode_w(int kbcode, int make)
  1468. {
  1469.     pokey_kbcode_w(0, kbcode, make);
  1470. }
  1471.  
  1472. void pokey2_kbcode_w(int kbcode, int make)
  1473. {
  1474.     pokey_kbcode_w(1, kbcode, make);
  1475. }
  1476.  
  1477. void pokey3_kbcode_w(int kbcode, int make)
  1478. {
  1479.     pokey_kbcode_w(2, kbcode, make);
  1480. }
  1481.  
  1482. void pokey4_kbcode_w(int kbcode, int make)
  1483. {
  1484.     pokey_kbcode_w(3, kbcode, make);
  1485. }
  1486.  
  1487.